// 引入SDK核心类
const app = getApp()
import { requestApi } from '../../api'
import { formatImage } from '../../utils/formatImage';

Page({
    data: {
        shopName: '',
        addr: '',
        location: null,
        indicatorDots: true,
        vertical: false,
        autoplay: true,
        interval: 3000,
        duration: 500,
        circular: true,
        shopInfo: null,
        shop_banner_image: [],
        shop_brand_list: [],
        uid: '',
        loading: true,
        channel: '',
        scene: '',
        sysWidth: wx.getSystemInfoSync().windowWidth
    }, 
    onLoad:function(opt) {
        let channel = opt && opt.channel || 'wx_xcx';
        this.setData({
            shop_id: opt.shop_id,
            seller_uid: opt.seller_uid,
            channel
        });
    },
    onShow: function () {
        const { scene } = wx.getLaunchOptionsSync();

        if (scene == 1047 && !app.toLogin()) {
            this.setData({
                loading: true
            })
            return false
        } else {
            const { shop_id, seller_uid } = this.data;
            this.setData({
                loading: false,
                scene
            })

            if (shop_id) {
                let userinfo = wx.getStorageSync('userinfo') || {};
    
                this.setData({
                    uid: userinfo.uid,
                });
                this.getShopDetail(userinfo.uid, shop_id, seller_uid);
            }
        }
    },
    getShopDetail(uid, id, seller_uid) {
        const self = this;

        app.request({
            url: requestApi.wxShop,
            login: false,
            data: {
                id: id
            },
        }, (data) => {
            let d = data.original;
            if (d.status == 'success') {
                this.setData({
                    loading: true
                })
                if(d.data.id) {
                    d.data.shop_banner_image.forEach(function (item) {
                        item.path_full = formatImage.cdnImage(item.path)
                        switch(item.resourceType) {
                            case '1': // 专题
                                item.path_url = '/pages/topic/topicDetail/topicDetail?tid=' + item.resourceId + '&channel=' + self.data.channel;
                                break;
                            case '2': // 合集
                                item.path_url = '/pages/activity/activityDetail/activityDetail?tId=' + item.resourceId + '&channel=' + self.data.channel;
                                break;
                            case '4': // 商品
                                item.path_url = '/pages/goods/goodsDetail/goodsDetail?gid=' + item.resourceId + '&channel=' + self.data.channel;
                                break;
                        }
                    })
    
                    let brands = d.data.brandList, list = [];
    
                    brands.forEach(function (item) {
                        if (item.brand_img) {
                            item.brand_img.path_full = formatImage.cdnImage(item.brand_img.path);
                            list.push(item);
                        }
                    })
    
                    this.setData({
                        shopName: data.original.data.seller_nickname,
                        addr: data.original.data.shop_address,
                        location: data.original.data.location,
                        shopInfo: data.original.data,
                        shop_banner_image: d.data.shop_banner_image,
                        shop_tag_list: d.data.tag_list,
                        shop_brand_list: list,
                        indicatorDots: d.data.shop_banner_image.length > 1 ? true : false
                    });
                    const { scene } = this.data;
                    if (scene == 1047) {
                        this.setUserFlashName(uid, data.original.data.shop_tag);
                    }
                } else {
                    wx.redirectTo({
                      url: '/pages/store/store?seller_uid=' + seller_uid,
                    })
                }
            }
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        })
    },
    formSubmit() {
        const { addr, shopName, location } = this.data;

        wx.getLocation({
            type: 'gcj02',
            success (res) {
                wx.openLocation({
                    name: shopName,
                    address: addr,
                    latitude: location.lat * 1,
                    longitude: location.lng * 1,
                    scale: 16
                })
            },
            fail(err) {
                wx.showModal({
                    title: '是否授权当前位置',
                    content: '需要获取您的地理位置，请确认授权，否则无法获取您所需数据',
                    success: function (res) {
                        if (res.cancel) {
                            wx.showToast({
                              title: '授权失败',
                              icon: 'none',
                              duration: 1000
                            })
                        } else if (res.confirm) {
                            console.log(res);
                            wx.openSetting({
                                success: function (dataAu) {
                                    if (dataAu.authSetting["scope.userLocation"] == true) {
                                        wx.openLocation({
                                            name: shopName,
                                            address: addr,
                                            latitude: location.lat * 1,
                                            longitude: location.lng * 1,
                                            scale: 16
                                        })
                                    }
                                }
                            })  
                        }
                    },
                    fail(e) {
                        console.log(e);
                    }
                });
                  
            }
        })
    },
    goBrand(e) {
        const { id, uid } = e.currentTarget.dataset;

        wx.navigateTo({
            url: '../brands/brands?brand_id=' + id + '&seller_uid=' + uid,
        })
    },
    goDetailPage(e) {
        const { url } = e.currentTarget.dataset;
        if (url) {
            wx.navigateTo({
                url: url,
            })
        }
    },
    setUserFlashName(id, shop_name) {
        app.request({
            url: requestApi.setUserFlashName,
            login: false,
            data: {
                login_uid: id,
                flash_shop_tag: shop_name
            },
        }, (data) => {
            let d = data.original;
            if (d.status == 'success') {

            }
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        })
    },
})